package org.piaohao.controller;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;
import org.piaohao.model.Driver;
import org.piaohao.model.Line;
import org.piaohao.model.Organization;
import org.piaohao.model.Vehicle;
import org.piaohao.util.string.IdCardGenerator;
import org.piaohao.util.string.StringUtil;
import org.piaohao.util.string.UUIDGenerator;
import org.piaohao.util.time.TimeUtil;

/**
 *
 * @author piaohao
 */
public class AdminController extends Controller {

    public static final String moduleName = "管理员模块";

    public void initDriver() {
        try {
            Db.update("delete from t_line");
            List<Organization> orgs = Organization.dao.find("select * from t_organization where id<>?", "0");
            String[] education = new String[]{"小学", "初中", "中专", "高中", "大专", "本科", "硕士", "博士", "博士后"};
            for (int i = 0; i < 2000; i++) {
                Driver driver = new Driver();
                driver.set("id", UUIDGenerator.getUUID());
                driver.set("name", StringUtil.generateChineseName());
                Date birthday = TimeUtil.generateBirthday();
                Calendar c = Calendar.getInstance();
                int nowYear = c.get(Calendar.YEAR);
                c.setTime(birthday);
                int age = nowYear - c.get(Calendar.YEAR);
                driver.set("age", age);
                driver.set("birthday", new Timestamp(birthday.getTime()));
                driver.set("sex", new Random().nextInt(2) == 0 ? "男" : "女");
                driver.set("id_card", IdCardGenerator.generate());
                driver.set("cellphone", StringUtil.generatePhone());
                driver.set("email", StringUtil.generateEmail());
                driver.set("education", education[new Random().nextInt(education.length)]);
                driver.set("org_id", orgs.get(new Random().nextInt(orgs.size())).getStr("id"));
                driver.save();
            }
            setAttr("msg", "数据插入操作完成！");
            render("/pages/admin/index.jsp");
        } catch (Exception e) {
            e.printStackTrace();
            setAttr("msg", "数据插入操作失败！");
            render("/pages/admin/index.jsp");
        }
    }

    public void initDriverPhone() {
        try {
            List<Driver> drivers = Driver.dao.find("select * from t_driver");
            for (int i = 0; i < drivers.size(); i++) {
                Driver driver = drivers.get(i);
                driver.set("cellphone", StringUtil.generatePhone());
                driver.update();
            }
            setAttr("msg", "数据插入操作完成！");
            render("/pages/admin/index.jsp");
        } catch (Exception e) {
            e.printStackTrace();
            setAttr("msg", "数据插入操作失败！");
            render("/pages/admin/index.jsp");
        }
    }

    public void initVehicle() {
        try {
            Db.update("delete from t_vehicle");
            List<Organization> orgs = Organization.dao.find("select * from t_organization where id<>?", "0");
            String[] type = new String[]{"普通级轿车", "中级轿车", "高一级轿车", "高二级轿车", "小型普通级", "小型中级",
                "小型高一级", "小型高二级", "中型普通级", "中型中级", "中型高一级", "中型高二级", "大型普通级", "大型中级",
                "大型高一级", "大型高二级", "大型高三级", "大型卧铺中级", "大型卧铺高一级", "大型卧铺高二级"};
            String[] color = new String[]{"红色", "白色", "黑色", "蓝色", "黄色", "紫色", "褐色", "绿色"};
            for (int i = 0; i < 2000; i++) {
                Vehicle vehicle = new Vehicle();
                vehicle.set("id", UUIDGenerator.getUUID());
                vehicle.set("plate_number", StringUtil.generatePlateNumber());
                vehicle.set("veh_reg_date", TimeUtil.generateVehRegDate());
                vehicle.set("type", type[new Random().nextInt(type.length)]);
                vehicle.set("person_count", new Random().nextInt(30) + 30);
                vehicle.set("private_number", StringUtil.generateNumber(4));
                vehicle.set("road_trans_num", StringUtil.generateNumber(7));
                vehicle.set("plate_color", new Random().nextInt(2) == 0 ? "蓝色" : "黄色");
                vehicle.set("veh_color", color[new Random().nextInt(color.length)]);
                vehicle.set("factory", StringUtil.generateChinese(7));
                vehicle.set("model", StringUtil.generateVehModel());
                vehicle.set("org_id", orgs.get(new Random().nextInt(orgs.size())).getStr("id"));
                vehicle.save();
            }
            setAttr("msg", "数据插入操作完成！");
            render("/pages/admin/index.jsp");
        } catch (Exception e) {
            e.printStackTrace();
            setAttr("msg", "数据插入操作失败！");
            render("/pages/admin/index.jsp");
        }
    }

    public void initLine() {
        try {
            Db.update("delete from t_line");
            List<Organization> orgs = Organization.dao.find("select * from t_organization where id<>?", "0");
            String[] type = new String[]{"省际", "市际", "县际", "县内", "公交", "旅游", "超长线"};
            String[] level = new String[]{"一类", "二类", "三类", "四类"};
            String[] place = new String[]{"万州区", "涪陵区", "渝中区", "大渡口", "江北区", "沙坪坝", "九龙坡", "南岸区",
                "北碚区", "万盛区", "双桥区", "渝北区", "巴南区", "黔江区", "长寿区", "江津区", "合川区", "永川区", "南川区",
                "綦江县", "潼南县", "铜梁县", "大足县", "荣昌县", "璧山县", "梁平县", "城口县", "丰都县", "垫江县", "武隆县",
                "忠县", "开县", "云阳县", "奉节县", "巫山县", "巫溪县", "石柱县", "秀山县", "酉阳县", "彭水县"};
            for (int i = 0; i < 200; i++) {
                Line line = new Line();
                line.set("id", UUIDGenerator.getUUID());
                int index = new Random().nextInt(place.length);
                String startSta = place[index];
                int index2 = new Random().nextInt(place.length);
                while (index2 == index) {
                    index2 = new Random().nextInt(place.length);
                }
                String endSta = place[index2];
                String name = startSta + "—" + endSta;
                line.set("start_station", startSta);
                line.set("end_station", endSta);
                line.set("name", name);
                line.set("mileage", new Random().nextInt(500) + 50);
                line.set("line_start_date", TimeUtil.generateLineStartDate());
                line.set("type", type[new Random().nextInt(type.length)]);
                line.set("level_", level[new Random().nextInt(level.length)]);
                line.set("org_id", orgs.get(new Random().nextInt(orgs.size())).getStr("id"));
                line.save();
            }
            setAttr("msg", "数据插入操作完成！");
            render("/pages/admin/index.jsp");
        } catch (Exception e) {
            e.printStackTrace();
            setAttr("msg", "数据插入操作失败！");
            render("/pages/admin/index.jsp");
        }
    }
}
